package com.coolwar.netty.intranet;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ClientProxy {

    public static void main(String[] args) throws IOException, InterruptedException {
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventExecutors)
                .channel(NioSocketChannel.class)
                .handler(new ClientHandler());
        ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080);
        channelFuture.addListener((ChannelFutureListener) future -> {
            if (future.isSuccess()) {
                System.out.println("连接成功");
                future.channel().writeAndFlush("client10010".getBytes());
            }
        });

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            channelFuture.channel().writeAndFlush(in.readLine() + "\r\n");
        }
    }

}
